<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\UsersReg;
use App\Models\Chuban;
use App\Models\Jobs;
use App\Models\UsersAttrib;
use App\Models\UsersFollow;
use App\Models\Category as cat;
use App\Models\BookTempstorage;
use App\Models\Book;
use App\Models\Bags;
use App\Models\BooksReview;
use App\Models\Shudan;
use App\Models\mall\Ecy_users; //商城会员
use Session;
use Redirect;

class UcenterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($uid)
    {
        //会员中心首页
        $this->umenu($uid);
        return view('ucenter.layouts.index');
    }   

    public function fellow($uid)
    {
        //关注的会员列表
        $this->umenu($uid);
        return view('ucenter.layouts.fellow');
    }

    public function fans($uid)
    {
        //粉丝的会员列表
        $this->umenu($uid);
        return view('ucenter.layouts.fans');
    }

    public function reviewslist($uid)
    {
        //会员中心长书评列表
        $this->umenu($uid);
        return view('ucenter.layouts.reviewslist');
    }

    public function profile($uid) {
        //会员中心会员资料修改
        //判断是否修改自己的资料
        if(Session::get('nameid')==$uid)
        {
            $users = UsersReg::find($uid);
            $jobs = Jobs::all();
            $usersattrib = UsersAttrib::where('uid', $uid)->first();
            //生日年月日
            $timestamp=$users->birthday;
            // date( 'y-m-d',time() )是有0的格式 
            // date( 'y-n-j',time() )是没0的格式
            $datetime=date('Y-n-j', $timestamp);
            $birthday = explode('-', $datetime);
            return view('ucenter.admin.profile',array(
                'users' => $users, 
                'jobs' => $jobs,
                'usersattrib' => $usersattrib,
                'birthday' => $birthday
                ));
        }
        else {
            // 跳转到要访问的会员中心
            return redirect('u/'.$uid);;
        }
    }

    public function saveprofile(Request $request) {
        //保存会员修改的资料
        $uid = Session::get('nameid');
        $users = UsersReg::find($uid);
        $usersattrib = UsersAttrib::where('uid', $uid)->first();
        //判断数据库中是否有签名和qq
        if(empty($usersattrib)) {
            $usersattrib = new UsersAttrib;
            $usersattrib->uid = $uid;
            $usersattrib->signature = request::input('signature');
            $usersattrib->shopjieshao = request::input('shopjieshao');
            $usersattrib->qq = request::input('qq');
            $usersattrib->save();
        }
        else {
            $usersattrib->signature = request::input('signature');
            $usersattrib->shopjieshao = request::input('shopjieshao');
            $usersattrib->qq = request::input('qq');
            $usersattrib->save();
        }

        $users->sex = request::input('sex');
        $year = request::input('year');
        $month = request::input('month');
        $day = request::input('day');
        $birthday = $year. '-' . $month . '-' . $day;
        $birthday = strtotime($birthday); //2009-1-22 转换结果：1232553600
        $users->birthday = $birthday;
        $users->province = request::input('province');
        $users->city = request::input('city');
        $users->job_id = request::input('job_id');
        // $users->mobile = request::input('mobile');
        $users->save();

        return 'ok'; //ajax 使用
    }

    public function chpwd($uid)
    {
        //会员中心修改密码页面
        //判断是否修改自己的资料
        if(Session::get('nameid')==$uid)
        {
            $users = UsersReg::find($uid);
            $usersattrib = UsersAttrib::where('uid', $uid)->first();
            
            return view('ucenter.admin.chpwd',array('users' => $users));
        }
        else {
            // 跳转到要访问的会员中心
            return redirect('u/'.$uid);;
        }
    }

    public function ischeckchpwd()
    {
        //判断输入的旧密码是否正确
        $oldpwd = trim(request::input('oldpwd')); //获得输入密码
        $uid = Session::get('nameid');
        $users = UsersReg::find($uid);

        $password = $users->password; //用户的密码
        $salt = $users->salt;

        $oldpwd = $this->compile_password($oldpwd, $salt); // 加密后的密码

        if($oldpwd == $password){
            return '正确';
        }else{
            return '错误';
        }
    }

    public function savechpwd()
    {
        //保存会员修改的新密码        
        $newpwd = trim(request::input('newpwd'));
        $uid = Session::get('nameid');
        $users = UsersReg::find($uid);
        $salt = $users->salt;
        $newpwd = $this->compile_password($newpwd, $salt); // 对新密码加密
        $users->password = $newpwd;
        $users->save();

        //保存修改商城会员的密码
        $ecy_users = Ecy_users::where('user_name', Session::get('name'))->first();
        $ecy_users->password = md5(trim(request::input('newpwd')));
        $ecy_users->ec_salt = null; //必须
        $ecy_users->save();
        //保存修改商城会员的密码end

        return 'ok';
    }

    public function fellowuser($fuid) {
        //关注会员
        $nameid = Session::get('nameid');
        $usersfollow = UsersFollow::where('fans_uid', $nameid)->where('friend_uid', $fuid)->get();
        if(count($usersfollow)==0) //已经关注了不在关注
        {
            $usersfollow = new UsersFollow;
            $usersfollow->fans_uid = $nameid;
            $usersfollow->friend_uid = $fuid;
            $usersfollow->add_time = time(); 
            $usersfollow->save();
        }
        return Redirect::back();
    }

    public function unfellowuser($fuid){
        //取消关注会员
        $nameid = Session::get('nameid');
        $usersfollow = UsersFollow::where('fans_uid', $nameid)->where('friend_uid', $fuid)->delete();
        return Redirect::back();
    }

    public function bagslist($uid)
    {
        //转让或出借的图书列表
        $this->umenu($uid);

        //每页显示15个记录
        $pre_page = 6;
        $bags = Bags::where('uid',$uid)->orderBy('add_time', 'desc')->paginate($pre_page);
        //统计图书的数量
        $bagscount = Bags::where('uid',$uid)->count();

        foreach($bags as $bag)
        {
            //为了得到图书的信息
            $bag['books'] = $bag->hasOnebooks;
        }

        return view('ucenter.layouts.bags', array(
            'bags'       => $bags,
            'bagscount'  => $bagscount
            ));
    }


    // ------share------
    public function umenu($uid)
    {
        //会员中心 菜单共享视图
        $users = UsersReg::find($uid);
        $uas = UsersAttrib::where('uid', $uid)->first();

        //出版社审核状态
        $chuban = Chuban::where('uid', $uid)->first();
        if(!empty($chuban)){
            $chuban_mark = $chuban->mark;
        }else{
            $chuban_mark = 0;
        }

        $guanzhus = UsersFollow::where('fans_uid', $uid)->get(); // 获得关注会员
        foreach($guanzhus as $guanzhu)
        {
            //为了得到关注会员名字和头像
            $guanzhu['users'] = $guanzhu->hasOneguanzhus;

            // 判断会员是否被关注 start
            $gzs = UsersFollow::where('fans_uid', Session::get('nameid'))->where('friend_uid', $guanzhu->friend_uid)->get();
            if(count($gzs)!=0)
            {
                $guanzhu['gzmark'] = 1; ////已经关注
            }
            else
            {
                $guanzhu['gzmark'] = 0; ////没有关注
            }
            // end         
        }

        $fensis = UsersFollow::where('friend_uid', $uid)->get(); // 获得粉丝会员
        foreach($fensis as $fensi)
        {
            //为了得到粉丝会员名字和头像
            $fensi['users'] = $fensi->hasOnefensis;

            // 判断会员是否被关注 start
            $gzs = UsersFollow::where('fans_uid', Session::get('nameid'))->where('friend_uid', $fensi->fans_uid)->get();
            if(count($gzs)!=0)
            {
                $fensi['gzmark'] = 1; ////已经关注
            }
            else
            {
                $fensi['gzmark'] = 0; ////没有关注
            }
            // end 
        }


        $booksreviews =BooksReview::where('uid', $uid)->get(); //本会员($uid)的长评论

        foreach($booksreviews as $booksreview)
        {
            //为了得到图书的信息
            $booksreview['books'] = $booksreview->hasOnebooks;
        }

        $shudans =Shudan::where('uid', $uid)->get(); //本会员($uid)添加的书单

        if(!empty($users)){
            //view()->share('udata', [$users, $uas, $guanzhus, $fensis]); //{{$updata[0]-> }}
            //view()->share('users', $users); // {{$user[0]-> }}
            view()->share('users', $users);
            view()->share('uas', $uas);
            view()->share('guanzhus', $guanzhus);
            view()->share('fensis', $fensis); 
            view()->share('booksreviews', $booksreviews); 
            view()->share('shudans', $shudans);
            view()->share('chuban_mark',$chuban_mark);          
        }
        else{            
            //用户不存在,页面跳转到
            return abort('404');
        }
    } 

    /**
     * 根据 salt 混淆密码
     *
     * @param  string
     * @param  string
     * @return string
     */
    public function compile_password($password, $salt)
    {
        $password = md5(md5($password) . $salt);

        return $password;
    }

}
